"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 极致RTO-数据库系统类
Case Name   : 开启极致RTO，TPCC业务压力过程中，备机异常退出后恢复；业务压力后，关闭极致RTO，主备数据一致
Create At   : 2020/10/12
Owner       : @peilinqian
Description :
    1、开启极致RTO
    2、运行TPCC
    3、查询主备状态
    4、备机数据库进程退出kill -9
    5、查询主备状态
    6、TPCC进程是否异常
    7、等待10S重启数据库
    8、查询主备状态
    9、TPCC执行完成后，关闭极致RTO
    10、查询主备数据是否一致
Expect      :
    1、极致RTO开启成功
    2、TPCC正常运行
    3、主备状态正常
    4、kill -9成功
    5、主机状态正常、备机stop
    6、TPCC进程正常
    7、重启数据库
    8、主备状态正常
    9、极致RTO关闭成功
    10、主备数据一致
History     :
    modified：2021-3-9 by 5328113;主备同步方法调用修改，增加超时等待时间；
    modified：2021-2-5 by 5328113;guc参数还原时指定还原值
    modified:2021-1-28 by 5328113;tpcc执行完成增加主备同步，删除start成功断言，删除不必要tpcc
"""

import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ExtremeRto(unittest.TestCase):


    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_RTO_Dbsys_Case002:初始化----')
        self.pri_root = Node(node='PrimaryRoot')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.sta1_root = Node(node='Standby1Root')
        self.sta1_dbuser = Node(node='Standby1DbUser')
        self.sta2_dbuser = Node(node='Standby2DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.sta_2_sh = CommonSH('Standby2DbUser')
        self.com = Common()
        self.constant = Constant()
        self.tpcc_path = macro.TPCC_PATH
        self.node_num = self.com.get_node_num(self.pri_root)
        self.log.info(self.node_num)
        self.max_time = 1200
        self.min_time = 300

    def test_main(self):
        try:
            self.log.info('----1、开启极致RTO----')
            msg = self.pri_sh.executeGsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'replication_type=1')
            self.assertTrue(msg)
            msg = self.pri_sh.executeGsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'recovery_parse_workers=4')
            self.assertTrue(msg)
            msg = self.pri_sh.executeGsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'recovery_redo_workers=4')
            self.assertTrue(msg)
            msg = self.pri_sh.executeGsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'hot_standby=off')
            self.assertTrue(msg)
            # 重启数据库配置文件生效
            self.pri_sh.stopDbCluster()
            time.sleep(2)
            self.pri_sh.startDbCluster()

            self.log.info('----查看主机query，同步是否正常----')
            self.pri_sh.check_location_consistency('primary',
                                                   self.node_num,
                                                   self.min_time)

            self.log.info('----查询主备状态----')
            status = self.pri_sh.getDbClusterStatus('status')
            self.assertTrue(status)

            self.log.info('----2、运行TPCC----')
            tpcc_cmd = f'. runBenchmark.sh {macro.TPCC_PROPS_FILENAME}'
            tpcc_thread = ComThread(self.com.startTPCC,
                                    args=(self.pri_root,
                                          self.tpcc_path,
                                          tpcc_cmd,))
            tpcc_thread.setDaemon(True)
            tpcc_thread.start()
            time.sleep(2)

            self.log.info('----3、查询主备状态----')
            self.pri_sh.getDbClusterStatus('status')

            self.log.info('-----4、备机数据库进程退出kill -9----')
            self.com.kill_pid_keyword('gaussdb', 9, self.sta1_dbuser)
            time.sleep(1)

            self.log.info('----查询备机数据数据进程状态----')
            db_process_cmd = 'ps ux | grep \'M standby\' | grep -v grep'
            self.log.info(db_process_cmd)
            db_process_result = self.sta1_dbuser.sh(db_process_cmd).result()
            self.log.info(db_process_result)

            self.log.info('-----5、查询主备状态----')
            status = self.pri_sh.getDbClusterStatus('status')
            self.assertFalse(status)

            self.log.info('-----6、TPCC进程是否异常----')
            # 此处不做断言，因为tpcc执行时间不固定，只断言tpcc执行结果
            tpcc_process_cmd = 'ps ux | grep jTPCC | grep -v grep'
            self.log.info(tpcc_process_cmd)
            tpcc_process_resul = self.pri_root.sh(tpcc_process_cmd).result()
            self.log.info(tpcc_process_resul)

            self.log.info('-----7、等待10S重启数据库----')
            time.sleep(10)
            self.sta_1_sh.startDbInstance('standby')

            self.sta_1_sh.executeGsctl('query', '', get_detail=True)

            self.log.info('-----TPCC结果验证----')
            # 等待TPCC执行完成
            tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
            tpcc_result = tpcc_thread.get_result()
            self.log.info(tpcc_result)
            self.assertIn(self.constant.TPCC_SUCCESS_MSG, tpcc_result)

            self.log.info('----查看主机query，同步是否正常----')
            self.pri_sh.check_location_consistency('primary',
                                                   self.node_num,
                                                   self.max_time)

            self.log.info('-----8、查询主备状态----')
            status = self.pri_sh.getDbClusterStatus('status')
            self.assertTrue(status)

        finally:
            self.log.info('-----9、TPCC执行完成后，关闭极致RTO----')
            msg = self.pri_sh.executeGsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'recovery_parse_workers = 1')
            self.assertTrue(msg)
            msg = self.pri_sh.executeGsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'recovery_redo_workers = 1')
            self.assertTrue(msg)
            msg = self.pri_sh.executeGsguc('set',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           'hot_standby=on')
            self.assertTrue(msg)
            # 重启数据库配置文件生效
            self.pri_sh.stopDbCluster()
            time.sleep(2)
            self.pri_sh.startDbCluster()

            # 校验备机是否完成数据同步
            self.log.info('校验备机是否完成数据同步')
            flag_1 = self.sta_1_sh.check_location_consistency('standby',
                                                              self.node_num,
                                                              self.min_time)
            self.assertTrue(flag_1)
            flag_2 = self.sta_2_sh.check_location_consistency('standby',
                                                              self.node_num,
                                                              self.min_time)
            self.assertTrue(flag_2)

            self.log.info('----查询主备状态----')
            status = self.pri_sh.getDbClusterStatus('status')
            self.assertTrue(status)

            self.log.info('-----10、查询主备数据是否一致----')
            nodes_tuple = (self.pri_dbuser,
                           self.sta1_dbuser,
                           self.sta2_dbuser)
            flag = self.com.check_data_sample_by_all(r'\d', *nodes_tuple)
            self.assertTrue(flag)

            table_dict = self.com.format_sql_result(
                self.pri_sh.executDbSql(r'\d'))
            table_name = table_dict.get('Name')
            for name in table_name:
                select_sql = f'select count(*) from {name};'
                flag = self.com.check_data_sample_by_all(select_sql,
                                                         *nodes_tuple)
                self.assertTrue(flag)

    def tearDown(self):
        self.log.info('-----校验刷脏页是否完成----')
        self.pri_sh.check_whether_remain_dirty_page()

        self.log.info(
            '-----Opengauss_Reliability_RTO_Dbsys_Case002:执行完毕----')
